package com.plant.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.plant.pojo.Photo;
import com.plant.pojo.Plant;
import com.plant.service.PhotoService;
import com.plant.service.PlantService;
import com.plant.util.ExcelImport;
import com.plant.util.ExcelUtils;
import com.plant.util.MyNameFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.lang.reflect.Field;
import java.util.List;

@Controller
@RequestMapping("import")
public class ImportController {
    // 8.130.8.204
    // localhost
    // /home/map/
    // D:\tomcat-mapping\
    private static final String ADDRESS = "8.130.8.204";
    private static final String PATH  = "/home/map/";

    @Autowired
    @Qualifier("plantService")
    private PlantService plantService;
    @Autowired
    @Qualifier("photoService")
    private PhotoService photoService;

    @RequestMapping(value = "importExcel", method = RequestMethod.POST)
    @ResponseBody
    private String importExcel( MultipartFile file) throws Exception {
        List<Plant> list = ExcelUtils.readMultipartFile(file, Plant.class);
        System.out.println("list size" +list.size()+"----------");
        int size = 0;
        for (Plant plant : list) {
            System.out.println("6666666666");
                Plant plant1 = plantService.queryByRn(plant.getResourceNumber());
            System.out.println("7777777777777777777777");
                if (plant1 != null) {
                    plantService.delete(plant.getResourceNumber());
                    photoService.delete(plant.getResourceNumber());
                }
                int i = plantService.addPlant(plant);
                System.out.println("插入结果：" + i+"---"+plant.toString());
                if (i == 1){
                    size++;
                }
        }
        return size+"";
    }
    @RequestMapping(value = "uploadPhotos" , method = RequestMethod.POST)
    @ResponseBody
    private String addPhoto(String germplasmName , String resourceNumber, MultipartFile[] imageList) throws IOException {
        if (imageList == null) return "empty";
        System.out.println("germplasmName :"+germplasmName+"image list size:" + imageList.length);
        Photo photo = new Photo();
        photo.setResourceNumber(resourceNumber);
        try {
            photoService.delete(resourceNumber);
            if (imageList.length == 1) {
                String name =resourceNumber+ "-" + germplasmName + "one.jpg";
                String path = PATH + name;
                String readPath = "http://" + ADDRESS + ":8080/map/" + name;
                photo.setOphoto(readPath);
                photoService.addPhoto(photo);
                File file = new File(path);
                if (file.exists()){
                    boolean delete = file.delete();
                }
                imageList[0].transferTo(new File(path));

            }
            if (imageList.length == 2) {
                String name1 = photo.getResourceNumber() + "-" + germplasmName + "one.jpg";
                String name2 = photo.getResourceNumber() + "-" +germplasmName + "two.jpg";
                String path1 = PATH + name1;
                String path2 = PATH + name2;
                String readPath1 = "http://" + ADDRESS + ":8080/map/" + name1;
                String readPath2 = "http://" + ADDRESS + ":8080/map/" + name2;
                photo.setOphoto(readPath1);
                photo.setTphoto(readPath2);
                photoService.addPhoto(photo);
                File file1 = new File(path1);
                if (file1.exists()){
                    boolean delete = file1.delete();
                }
                File file2 = new File(path2);
                if (file2.exists()){
                    boolean delete = file2.delete();
                }
                imageList[0].transferTo(new File(path1));
                imageList[1].transferTo(new File(path2));
            }
            if (imageList.length == 3) {
                String name1 = photo.getResourceNumber() + "-" + germplasmName + "one.jpg";
                String name2 = photo.getResourceNumber() + "-" + germplasmName + "two.jpg";
                String name3 = photo.getResourceNumber() + "-" + germplasmName + "three.jpg";
                String path1 = PATH + name1;
                String path2 = PATH + name2;
                String path3 = PATH + name3;
                String readPath1 = "http://" + ADDRESS + ":8080/map/" + name1;
                String readPath2 = "http://" + ADDRESS + ":8080/map/" + name2;
                String readPath3 = "http://" + ADDRESS + ":8080/map/" + name3;
                photo.setOphoto(readPath1);
                photo.setTphoto(readPath2);
                photo.setFphoto(readPath3);
                photoService.addPhoto(photo);
                File file1 = new File(path1);
                if (file1.exists()){
                    boolean delete = file1.delete();
                }
                File file2 = new File(path2);
                if (file2.exists()){
                    boolean delete = file2.delete();
                }
                File file3 = new File(path3);
                if (file3.exists()){
                    boolean delete = file3.delete();
                }
                imageList[0].transferTo(new File(path1));
                imageList[1].transferTo(new File(path2));
                imageList[2].transferTo(new File(path3));
            }
            return "ok";
        }catch (Exception e) {
            e.printStackTrace();
            return "no";
        }
    }



    @RequestMapping("queryById")
    @ResponseBody
    private String queryByRn(String resourceNumber) {
        Plant plant = plantService.queryByRn(resourceNumber);
        Photo photo = photoService.query(plant.getResourceNumber());
        if (photo != null) {
            if (photo.getOphoto() != null) {
                plant.setOphoto(photo.getOphoto());
            }
            if (photo.getTphoto() != null) {
                plant.setTphoto(photo.getTphoto());
            }
            if (photo.getFphoto() != null) {
                plant.setFphoto(photo.getFphoto());
            }
        }
        return JSON.toJSONString(plant, new MyNameFilter());
    }

    @RequestMapping("querybyspecies")
    @ResponseBody
    private String queryBySpecies(String species) {
        System.out.println("species" + species);
        List<Plant> plants = plantService.queryBySpecies(species);
        for (Plant plant : plants) {
            String resourceNumber = plant.getResourceNumber();
            System.out.println("resourceNumber" + resourceNumber);
            Photo photo = photoService.query(resourceNumber);
            if (photo != null) {
                if (photo.getOphoto() != null) {
                    plant.setOphoto(photo.getOphoto());
                }
                if (photo.getTphoto() != null) {
                    plant.setTphoto(photo.getTphoto());
                }
                if (photo.getFphoto() != null) {
                    plant.setFphoto(photo.getFphoto());
                }
            }

        }
        return JSON.toJSONString(plants);

    }

    @RequestMapping("change")
    @ResponseBody
    private void change(String resourceNumber, String name, String value) {

        Field[] fields = Plant.class.getDeclaredFields();

        if (name.indexOf("(") != -1) {
            name = name.substring(0, name.indexOf("("));
        }

        for (Field field : fields) {

            ExcelImport annotation = field.getAnnotation(ExcelImport.class);
            if (annotation != null) {
                if (annotation.value().equals(name)) {
                    plantService.update(resourceNumber, field.getName(), value);
                }
            }

        }

    }
    @RequestMapping(value = "test" ,method = RequestMethod.GET)
    @ResponseBody
 private String test(){
        return "ssss";
 }
    @RequestMapping(value = "test1" ,method = RequestMethod.POST)
    @ResponseBody
    private String test1(){
        return "ssss666";
    }
}
